package org.yn.dept.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yn.auth.annotation.PermissionAnnotation;
import org.yn.base.util.AjaxResult;
import org.yn.base.util.PageInfo;
import org.yn.dept.domain.Department;
import org.yn.dept.service.IDepartmentService;
import org.yn.query.DepartmentQuery;

import java.util.List;
import java.util.Objects;
@PermissionAnnotation(name="部门管理",descs = "部门管理api")
@RestController
@RequestMapping("/department")
@Api("部门管理接口")
public class DepartmentController {

    @Autowired
    IDepartmentService departmentService;

    /**
     * 查询全部
     */
    @PermissionAnnotation(name="查询所有部门",descs = "查询所有部门")
    @GetMapping()
    @ApiOperation(value = "查询所有部门")
    public AjaxResult selectAll() {
        try {
            List<Department> employee = departmentService.selectAll();
            return AjaxResult.success(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 查询单个
     */
    @PermissionAnnotation(name="通过id查询部门",descs = "通过id查询部门")
    @GetMapping("/{id}")
    @ApiOperation(value = "通过id查询部门")
    public AjaxResult selectById(@PathVariable Long id) {
        try {
            Department department = departmentService.selectById(id);
            return AjaxResult.success(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
     * 删除部门
     */
    @PermissionAnnotation(name="通过id删除部门",descs = "通过id删除部门")
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除部门")
    public AjaxResult delete(@PathVariable Long id) {
        try {
            departmentService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 新增
     */
    @PermissionAnnotation(name="新增或修改部门",descs = "新增或修改部门")
    @PostMapping()
    @ApiOperation(value = "新增或修改部门")
    public AjaxResult updateOrInsert(@RequestBody Department department) {
        try {
            if (Objects.nonNull(department)) {
                if (Objects.nonNull(department.getId())) {
                    departmentService.update(department);
                } else {
                    departmentService.insert(department);
                }
            } else {
                return AjaxResult.error("请输入正确的数据");
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 分页查询接口
     */
    @PermissionAnnotation(name="分页查询",descs = "分页查询接口")
    @PutMapping
    public AjaxResult queryPage(@RequestBody DepartmentQuery departmentQuery){
        try {
            PageInfo<Department> pageInfo = departmentService.queryPage(departmentQuery);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 批量删除部门
     * @param ids
     * @return
     */
    @PermissionAnnotation(name="批量删除部门",descs = "批量删除部门")
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 删除部门
     */
    @PermissionAnnotation(name="获取部门树",descs = "获取部门树")
    @GetMapping("/deptTree")
    @ApiOperation(value = "获取部门树")
    public AjaxResult deptTree() {
        try {
           List<Department> list= departmentService.getDeptTree();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

}